<?php
/*******************************************************************************
 * $Date:: 29 May 2010 00:20:45												   $
 * $Revision::																   $
 * $Author:: Tuan Duong													   	   $
 * $Comment::															   	   $
 ******************************************************************************/
class Bet_Admin_Controller extends Bet_ApplicationController
{

	public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response,
			array $invokeArgs = array()) {
		parent::__construct($request, $response, $invokeArgs);
		if ($this->requireLoggedIn()) {
			$identity = Zend_Auth::getInstance()->getIdentity();	
			if (empty($identity)) {
				$this->_redirect('/admin/auth/login');
			}
		}
	}

	static $menuData = array();
	public function init () {
		$config = Zend_Registry::get('config');
		Zend_Layout::startMvc(array('layoutPath' => $config['resources']['layout']['admin']['layoutPath']));

		$identity = Zend_Auth::getInstance()->getIdentity();
		if (!empty($identity)) {
			/* Initialize action controller here */
			if (0 == count(self::$menuData)) {
				//$this->_initMenu();
				// Edited by Duc: use Zend_Navigation to store menu data and intergrate it with Zend_Acl 
				$this->_initMenuNavigation();
			}
			$this->view->menuData = self::$menuData;
		}

		$layout = $this->getResource('layout');
		$view = $layout->getView();
		$view->addHelperPath("Bet/View/Helper","Bet_View_Helper");
	}

	protected function _initMenuNavigation() {
		if (Zend_Registry::isRegistered('navigation')) {
			$navigation = Zend_Registry::get('navigation');
		}
		$config = Zend_Registry::get('config');
		$menuConfigs = new Zend_Config_Xml($config['admin']['resources']['menu']['configFile'], 'nav');

		$navigation = new Zend_Navigation($menuConfigs);
		Zend_Registry::set('navigation', $navigation);

		$layout = $this->getResource('layout');
		$view = $layout->getView();
		$view->navigation($navigation);
	}

	private function _initMenu () {
		$xmlUtil = new Bet_Util_Xml(APPLICATION_PATH . "/configs/menu.xml");
		$menuTags = $xmlUtil->getXDoc()->getElementsByTagName("menu");

		foreach ($menuTags as $menuNode) {
			$menuId = $menuNode->getAttribute("id");
			$menuText = $menuNode->getAttribute("name");
			$menuLink = $menuNode->getAttribute("link");
			$mainMenu = new Bet_Bean_Menu($menuText, $menuLink, "");
			$mainMenu->submenu = array();
			$submenuTags = $menuNode->getElementsByTagName("submenu");
			foreach ($submenuTags as $submenuNode) {
				$submenu = new Bet_Bean_Menu();
				$submenu->bindNode($submenuNode);

				// Get submenu
				$childSubmenuTags = $submenuNode->getElementsByTagName("childsubmenu");
				if ($childSubmenuTags->length != 0) {
					$submenu->submenu = array();
					foreach ($childSubmenuTags as $childSubmenuNode) {
						$childSubmenu = new Bet_Bean_Menu();
						$childSubmenu->bindNode($childSubmenuNode);
						$submenu->submenu[] = $childSubmenu;
					}
				}
				$mainMenu->submenu[] = $submenu;
			}
			self::$menuData[$menuId] = $mainMenu;
		}
	}

	protected function requireLoggedIn($back = null) {
		return true;
	}

}




